/**
 * 
 */
package com.kudostar.android.kudostar;

import java.net.URISyntaxException;
import java.util.List;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.Resources;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.ContentURI;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Mike Luby
 * 
 */
public class SubmitKudo extends Activity {

	static final int KUDO_SENT_NOTIFY = 5;
	static final int NO_CONTACT_NOTIFY = 2;
	static final int NO_VALUE_NOTIFY = 1;
	static final int NOT_VALID_CONTACT_NOTIFY = 3;
	static final int PICK_CONTACT_REQUEST = 0;
	static final int PICK_FILE_REQUEST = 4;

	private String contactNumber = null;
	private ImageView contactphoto;
	private TextView contacttext;
	private Context context = this;
	private String FileData = null;
	private String imagePath = null;
	private Boolean isContactNumber = false;
	private KudoCommon kc = new KudoCommon( );
	private ImageButton n3, n2, n1, p1, p2, p3;
	private View phonenumber, phonenumberwrapper, contactwrapper, filewrapper;


	private int starValue = 0;

	protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
		/**
		 * Activity Result from File Browsing
		 */
		if( requestCode == PICK_FILE_REQUEST ) {
			if( resultCode == RESULT_OK ) {
				FileData = data;
				filewrapper.setVisibility( View.VISIBLE );
			}
		}
		/**
		 * Activity Result from Picking Contact
		 */
		if (requestCode == PICK_CONTACT_REQUEST) {
			if (resultCode == RESULT_OK) {
				ContentURI myPerson;
				try {
					myPerson = new ContentURI( data );
					// Query for this record.
					Cursor cur = managedQuery( myPerson, null, null, null );
					int phoneColumn = cur.getColumnIndex( android.provider.Contacts.PhonesColumns.NUMBER );
					int pathColumn = cur.getColumnIndex( android.provider.Contacts.PeopleColumns.PHOTO );
					int nameColumn = cur.getColumnIndex( android.provider.Contacts.PeopleColumns.NAME );
					while ( cur.next( ) ) {
						contactNumber = cur.getString( phoneColumn );
						imagePath = cur.getString( pathColumn );
						contactwrapper.setVisibility( View.VISIBLE );
						// Set contact name
						contacttext.setText( cur.getString( nameColumn ) );
						// Set contact photo
						// contactphoto.setImageURI( uri );
					}

				} catch (URISyntaxException e) {
					// TODO Auto-generated catch block
					e.printStackTrace( );
				}
			}
		}
	}

	public void onCreate(Bundle icicle) {
		super.onCreate( icicle );
		setContentView( R.layout.submit_kudo );
		phonenumber = findViewById( R.id.phonenumber );
		// Contact info
		contacttext = (TextView) findViewById( R.id.contacttext );
		contactphoto = (ImageView) findViewById( R.id.contactimage );
		contactwrapper = findViewById( R.id.contactwrapper );
		phonenumberwrapper = findViewById( R.id.phonenumberwrapper );
		filewrapper = findViewById( R.id.filewrapper );
		/**
		 * Select user by contacts
		 */
		findViewById( R.id.contacts ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				try {
					startSubActivity( new Intent( Intent.PICK_ACTION, new ContentURI( "content://contacts/people/" ) ), PICK_CONTACT_REQUEST );
					isContactNumber = false;
					phonenumberwrapper.setVisibility( View.GONE );
				} catch (URISyntaxException e) {
				}
			}
		} );
		/**
		 * Select user by entering phone number
		 */
		findViewById( R.id.phonenumbers ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				contactwrapper.setVisibility( View.GONE );
				isContactNumber = true;
				phonenumberwrapper.setVisibility( View.VISIBLE );
			}
		} );
		/**
		 * Set Star Value
		 */
		findViewById( R.id.n3 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = -3;
			}
		} );
		findViewById( R.id.n2 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = -2;
			}
		} );
		findViewById( R.id.n1 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = -1;
			}
		} );
		findViewById( R.id.p1 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = 1;
			}
		} );
		findViewById( R.id.p2 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = 2;
			}
		} );
		findViewById( R.id.p3 ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				starValue = 3;
			}
		} );
		/**
		 * Attach File button
		 */
		findViewById( R.id.attachfile ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				startSubActivity( new Intent( SubmitKudo.this, AndroidFileBrowser.class ), PICK_FILE_REQUEST );
			}
		} );
		/**
		 * Submit Star
		 * 
		 * Send Star request to site
		 */
		findViewById( R.id.ok ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				
				/**
				 * Submit Kudo to Site
				 */
				// Get Resources
				Resources res = context.getResources( );
				// Get Views
				EditText description = (EditText) findViewById( R.id.description );
				EditText phNum = (EditText) findViewById( R.id.phonenumber );
				EditText tags = (EditText) findViewById( R.id.tags );
				// Device Phone Number
				String deviceNumber = android.os.SystemProperties.get( android.telephony.TelephonyProperties.PROPERTY_LINE1_NUMBER );
				// Notify Manager
				NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
				// Set Value of KudoStar
				if (starValue != 0) {
					CharSequence contact = !isContactNumber ? contactNumber : phNum.getText( ).toString();
					// Contact is set
					if (contact != null) {
						// If contact is valid number
						if ( validatePhoneNumber( contact ) || !isContactNumber ) {
							//Phone Location
							LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
							// Get provider
							List<LocationProvider> providers = locMan.getProviders();
							LocationProvider provider = providers.get(0); 
							// Get Location
							Location curLoc = locMan.getCurrentLocation( provider.getName() ); 
							/**
							 * Send Data Here
							 */
							//Ok sent notification
							DataTransport dt = new DataTransport( );
							dt.SubmitStar( (String) contact, deviceNumber, starValue, description, tags, curLoc, FileData );
							nm.notifyWithText( KUDO_SENT_NOTIFY, res.getText( R.string.submit_kudo_sent ), NotificationManager.LENGTH_SHORT, null );
							finish();
						} else {
							nm.notifyWithText( NOT_VALID_CONTACT_NOTIFY, res.getText( R.string.submit_invalid_contact ), NotificationManager.LENGTH_SHORT, null );
						}
					} else {
						nm.notifyWithText( NO_CONTACT_NOTIFY, res.getText( R.string.submit_no_contact ), NotificationManager.LENGTH_SHORT, null );
					}
				} else {
					nm.notifyWithText( NO_VALUE_NOTIFY, res.getText( R.string.submit_no_value ), NotificationManager.LENGTH_SHORT, null );
				}
			}
		} );
		/**
		 * Cancel Submit
		 * 
		 * This just closes the activity
		 */
		findViewById( R.id.cancel ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				finish( );
			}
		} );
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		super.onCreateOptionsMenu( menu );
		return this.kc.BuildMenu( menu );
	}
	
	@Override
	public boolean onOptionsItemSelected( Item item ) {
		return this.kc.MenuOptionSelect( this, item );
	}
	
	public static boolean validatePhoneNumber( CharSequence contact ) {
		String numPattern = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";
		return contact.toString( ).matches( numPattern );
	}
}
